import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '@/components/home.vue'
import Wellcome from '@/components/wellcome.vue'
import Test from '@/view/test.vue'

//功能页面
import classlist from '@/view/classlist.vue'
import login from '@/view/login.vue'
import register from '@/view/register.vue'
import pubque from '@/view/pubque.vue'
import perque from '@/view/perque.vue'
import users from '@/view/users.vue'
import classdetail from '@/view/classdetail.vue'
import knowledge from '@/view/knowledge.vue'

// 引入加载条
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 配置
NProgress.inc()
NProgress.configure({     
    easing: 'ease',  // 动画方式    
    speed: 800,  // 递增进度条的速度    
    showSpinner: false, // 是否显示加载ico    
    trickleSpeed: 200, // 自动递增间隔    
    minimum: 0.1 // 初始化时的最小百分比
})

Vue.use(VueRouter)

// 所有人都可以访问
const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', name: 'login', component: login, meta: {title: '登陆'}, },
    { path: '/register', name: 'register', component: register, meta: {title: '注册'}, },
    // 访问home时，重定向到wellcome页
    { path: '/home', name: 'home', component: Home, redirect: '/wellcome', meta: {title: '控制台'}, 
  
        children: [
        { path: '/wellcome', name: 'wellcome', component: Wellcome, meta: {title: '控制台'}}, 
        { path: '/users', name: 'users', component: users, meta: {title: '用户管理'}}, 
        { path: '/classlist', name: 'classlist', component: classlist, meta: {title: '班级管理'}},
        { path: '/classdetail', name: 'classdetail', component: classdetail, meta: {title: '班级详情页'}}, 
        { path: '/pubque', name: 'pubque', component: pubque, meta: {title: '公共题库'}}, 
        { path: '/perque', name: 'perque', component: perque, meta: {title: '我的题库'}}, 
        { path: '/knowledge', name: 'knowledge',component: knowledge, meta:{title: '知识点'}}

      
        ] 
    },
]

const router = new VueRouter({
  routes
})

/**
 * 挂载路由守卫
 * to 代表将要访问的路径
 * from 代表从哪个路径跳转而来
 * next 是一个函数，next() 放行。   next('/login') 强制跳转
 */
router.beforeEach((to, from, next) => {
    // 加载条
    NProgress.start()

    //放行
    next() 
})


// 挂载路由守卫 - 访问结束
router.afterEach(() => {
    // 关闭加载条
    NProgress.done()
})

export default router
